#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include <unordered_map>

static const char *dictFileName = "./Dict.txt";

class Dict
{
    void Load()
    {
        std::ifstream ifs(dictFileName, std::ios::in);
        if (!ifs.is_open())
        {
            std::cout << "load error\n";
            exit(1);
        }
        std::string k, v;
        while (ifs >> k >> v)
        {
            k.pop_back();
            _dict[k] = v;
        }

    }
    void UnLoad()
    {
        std::ofstream ofs(dictFileName, std::ios::out);
        if (!ofs.is_open())
        {
            std::cout << "unload error\n";
            exit(2);
        }
        for (const auto& kv : _dict)
        {
            std::string message = kv.first + ": " + kv.second + '\n';
            ofs << message;
        }
    }

public:
    Dict()
    {
        Load();
    }
    void Addkv(const std::string &k, const std::string &v)
    {
        _dict[k] = v;
    }
    std::string Translate(const std::string &key)
    {
        if (_dict.count(key))
            return _dict[key];
        else
            return "None";
    }
    ~Dict()
    {
        UnLoad();
    }

private:
    std::unordered_map<std::string, std::string> _dict;
};